<?php
/**
 * 我的订单主控类
 * Author: 孙武 QQ:1228746736
 * Date: 2017/11/1
 * Time: 10:37
 */

namespace Home\Model;
class MyTicketModel extends \Think\Model{

    /**
     * 根据出发地和目的地查询车次信息
     * @param $from 出发地
     * @param $to 目的地
     */
    public function getAddrsInfoByFromAndTo($from,$to)
    {
        $map = array('addr_from' => $from, 'addr_to' => $to);
        $addrs = D('addr')->where($map)->select();
        return $addrs;
    }


    /**
     * 返回未支付的订单
     * 订单包括预定未支付，订票未支付
     * @return mixed
     */
    public function getHadNoPay()
    {
        //获取未支付的订单
        $map = array(
            'od_pay_status' => '0',
            'user_id'=>session('userId'),
            'od_status'=>0
            );
        $res = $this->where($map)->select();

        //获取已确认的预定订单
        $mapPre = array('od_pay_status' => '0', 'od_is_depart' => '1','user_id'=>session('userId'));
        $resPre = D('pre_order')->where($mapPre)->select();



        $finDate=array();
        foreach ($res as $v){
            $addrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$addrInfo;
            array_push($finDate,$v);
        }

        foreach ($res as $v){
            $preAddrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$preAddrInfo;
            array_push($finDate,$v);
        }


        return $finDate;
    }



    /**
     * 根据地址id获取地址信息
     * @param $addrId
     * @return mixed
     */
    private function getAddrInfoByAddrId($addrId)
    {
        return D('addr')->find($addrId);

    }

    /**
     * 根据地址id获取地址信息  并将时间转为:年月日时分秒星期
     * @param $addrId
     * @return mixed
     */
    private function getAddrInfoByAddrIdAndFormate($addrId)
    {
        $res=D('addr')->find($addrId);
        $res['addr_depart_time']=getFormateDateFromTimestamp($res['addr_depart_time']);
        return $res;

    }

    /**
     * 获取预约中的订单
     */
    public function getOrdering()
    {
        $map=array('od_is_depart'=>'0',
            'user_id'=>session('userId'));
        $res=D('pre_order')->where($map)->select();
        return $this->getAllInfoByOrder($res);

    }

    /**
     * 通过订单表的信息获取所有的信息
     * @param $orderInfo
     */
    private function getAllInfoByOrder($orderInfo){
        $finDate=array();
        foreach ($orderInfo as $v){
            $addrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$addrInfo;
            array_push($finDate,$v);
        }
        return $finDate;
    }


    public function getHadPay()
    {
        //获取未支付的订单
        $map = array('od_pay_status' => '1',
            'user_id'=>session('userId'));
        $res = $this->where($map)->select();

        //获取已确认的预定订单
        $mapPre = array('od_pay_status' => '1', 'od_is_depart' => '1',
            'user_id'=>session('userId'));
        $resPre = D('pre_order')->where($mapPre)->select();



        $finDate=array();
        foreach ($res as $v){
            $addrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$addrInfo;
            array_push($finDate,$v);
        }

        foreach ($res as $v){
            $preAddrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$preAddrInfo;
            array_push($finDate,$v);
        }


        return $finDate;
    }

    /**
     * 获取已取消的票
     * @return array
     */
    public function getHadCancel()
    {
        //获取未支付的订单
        $map = array('od_status' => '2',
            'user_id'=>session('userId'));
        $res = $this->where($map)->select();

        //获取已确认的预定订单
        $mapPre = array('od_status' => '2', 'od_is_depart' => '1',
            'user_id'=>session('userId'));
        $resPre = D('pre_order')->where($mapPre)->select();



        $finDate=array();
        foreach ($res as $v){
            $addrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$addrInfo;
            array_push($finDate,$v);
        }

        foreach ($res as $v){
            $preAddrInfo=$this->getAddrInfoByAddrIdAndFormate($v['od_addr_id']);
            $v['addrInfo']=$preAddrInfo;
            array_push($finDate,$v);
        }


        return $finDate;
    }
}